package com.itheima.controller;


import com.itheima.constant.MessageConstant;

import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;


import com.itheima.pojo.Video;
import com.itheima.service.SetmealService;
import com.itheima.service.VideoService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //使用JedisPool操作Redis服务
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private VideoService voidService;

    @Reference
    private SetmealService setmealService;


//查询套餐通过套餐id
    @RequestMapping("/findById")
    public  Result findById(@RequestParam Integer id){
       Setmeal data= setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,data);
    }

//查询中间表确定当前id套餐里的检查组
    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId( @RequestParam Integer  setmealId){
      List<Integer> ids=  setmealService.findCheckGroupIdsBySetmealId(setmealId);
       return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,ids);
    }

//通过id删除
    @RequestMapping("/deleteById")
    public  Result deleteById(@RequestParam Integer setmealId){
        //删除中间表
        setmealService.deleteGroupBySetmealId(setmealId);
        setmealService.deleteById(setmealId);
        //删除云端
        Setmeal setmeal = setmealService.findById(setmealId);
        String img = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.Delete_PIC_DB_RESOURCES,img);
        return new Result(true,"删除套餐成功");
    }

//修改(将中间表关系删除重建)

    //新增套餐
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setmealService.edit(setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改套餐失败");
        }
        return new Result(true,"修改套餐成功");
    }


//修改套餐其中修改了图片(走云端删除)
    @RequestMapping("/editAndDelteImg")
    public Result editAndDelteImg(@RequestParam("checkgroupIds") Integer[] checkgroupIds,
                                   @RequestParam("oldImgUrl") String oldImgUrl,
                                   @RequestBody Setmeal setmeal
    ){
        try{
            System.out.println(oldImgUrl);
            setmealService.editAndDelteImg(oldImgUrl,setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改套餐失败");
        }
        return new Result(true,"修改套餐成功");
    }






    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam MultipartFile imgFile){
        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();//获取文件原始名
        String extention = originalFilename.substring(originalFilename.lastIndexOf(".")-1);//这里就是后缀
        //添加uuid
        String fileName = UUID.randomUUID().toString() + extention;//文件名
        System.out.println(fileName);
        String dbimg;
        try {
             dbimg = QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将信息存到reids,防止垃圾数据(带网址)
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,dbimg);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        //图片上传成功
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,dbimg);

    }


    //新增套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.pageQuery(queryPageBean);
    }

    @RequestMapping("/getAllVoidTable/{setmealId}")
    public  Result getAllVoidTable(@PathVariable("setmealId") Integer setmealId ){
       List<Video> list= voidService.getAllVoidTable(setmealId);
       return new Result(true,"查询视频成功",list);

    }
}
